package com.myidis.servlet;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myidis.entity.Quota;
import com.myidis.entity.QuotaData;
import com.myidis.entity.QuotaDataItem;
import com.myidis.mapper.QuotaDataItemMapper;
import com.myidis.mapper.QuotaDataMapper;
import com.myidis.mapper.QuotaMapper;
import com.myidis.response.QuotaDataRes;

@Service
public class QuotaDataServlet {
	@Autowired
	private QuotaDataMapper quotaDataMapper;
	@Autowired
	private QuotaMapper quotaMapper;
	@Autowired
	private QuotaDataItemMapper quotaDataItemMapper;
	
	public List<QuotaDataRes> all() {
		return this.useful(quotaDataMapper.all());
	}
	
	private List<QuotaDataRes> useful(List<QuotaData> dataList) {
		List<QuotaDataRes> res = new ArrayList<QuotaDataRes>();
		for(QuotaData data: dataList) {
			Quota quota = quotaMapper.getOne(data.getQuota());
			QuotaDataItem item = quotaDataItemMapper.getOne(data.getDataItem());
			res.add(new QuotaDataRes(data, quota, item));
		}
		return res;
	}
	
	public QuotaData getOne(int id) {
		return quotaDataMapper.getOne(id);
	}
	 
	public int insert(QuotaData obj) {
		return quotaDataMapper.insert(obj);
	}
	
	public int insertAll(List<QuotaData> objs) {
		int count = 0;
		for (QuotaData obj: objs) {
			count += quotaDataMapper.insert(obj);
		}
		return count;
	}
	
	public int update(QuotaData data) {
		return quotaDataMapper.update(data);
	}
	
	public int delete(int id) {
		return quotaDataMapper.delete(id);
	}
	
	public int deleteAll(int[] ids) {
		int count = 0;
		for(int id: ids)
			count += quotaDataMapper.delete(id);
		return count;
	}
}

